import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/check_in_detail/api.dart";
import "package:ct_pa_majordomo/pages/check_in_detail/index.dart";
import "package:ct_pa_majordomo/pages/device/widgets/device_header.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";

class CheckInRecord extends StatefulWidget {
  final int roomId;
  const CheckInRecord({super.key, required this.roomId});

  @override
  State<StatefulWidget> createState() => _CheckInRecordState();

}

class _CheckInRecordState extends State<CheckInRecord> {

  List _list = [];

  Widget _renderFlag(String status) {
    return Container(
      width: 60,
      height: 20,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: const BorderRadius.only(topRight: Radius.circular(8), bottomLeft: Radius.circular(15)),
        color: status == "1" ? ThemeVars.colorPrimaryLight : ThemeVars.colorDisabled,
      ),
      child: Text(status == "1" ? "入住中" : "已退房", style: TextStyle(fontSize: 10, color: status == "1" ? ThemeVars.colorPrimary : Colors.white),),
    );
  }

  List<Widget> _renderList() {
    if (_list.isEmpty) {
      return [
        const Padding(
          padding: EdgeInsets.only(top: 60),
          child: Column(
            children: [
              Icon(Icons.hourglass_empty_outlined, size: 60, color: ThemeVars.colorPrimaryLight,),
              Text("暂无入住信息"),
            ],
          ),
        )
      ];
    }
    return List.generate(_list.length * 2 - 1, (index) {
      if (index.isOdd) {
        return const SizedBox(height: 12,);
      }
      Map item = _list[index ~/ 2];
      print(">>>>>>>>>>>>>${item["renterList"]}");
      Map mainRenter = item["renterList"].firstWhere((renter) => int.parse(renter["mainFlag"]) == 1);

      return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Navigator.push(context, CupertinoPageRoute(builder: (BuildContext context) {
            return CheckInDetail(checkInId: item["checkInId"]);
          }));
        },
        child: DecoratedBox(
          decoration: const BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(8)),
            color: Colors.white,
          ),
          child: Stack(
            children: [
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 12),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(mainRenter["name"],),
                        const SizedBox(height: 6,),
                        Text("${item["checkInStart"]} 至 ${item["checkInEnd"]}", style: TextStyle(color: ThemeVars.colorTextTer,),)
                      ],
                    ),
                    Icon(Icons.arrow_forward_ios, size: 20, color: ThemeVars.colorDisabled,)
                  ],
                ),
              ),
              Positioned(
                right: 0,
                top: 0,
                child: _renderFlag(item["status"]),
              ),
            ],
          ),
        ),
      );
    });
  }

  Future<void> _getCheckInRecord() async {
    Map resData = await checkInApi.getCheckInRecord(widget.roomId);
    setState(() {
      _list = resData["rows"] ?? [];
    });
  }

  @override
  void initState() {
    _getCheckInRecord();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DeviceHeader(
      title: "入住记录",
      showSearch: false,
      showFloatBtn: false,
      leading: IconButton(
        icon: Icon(Icons.arrow_back_ios),
        onPressed: () {
          Navigator.pop(context);
        },
      ),
      children: [
        Positioned(
          width: MediaQuery.of(context).size.width,
          top: 0,
          left: 0,
          child: Container(
            width: MediaQuery.of(context).size.width,
            decoration: const BoxDecoration(
              color: Colors.white,
            ),
            padding: const EdgeInsets.symmetric(horizontal: ThemeVars.paddingPage, vertical: 6),
            child: const SizedBox(
              height: 40,
              child: TextField(
                decoration: InputDecoration(
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(5)),
                      borderSide: BorderSide.none
                  ),
                  hintText: "请输入",
                  isDense: true,
                  filled: true,
                  fillColor: ThemeVars.colorBackground,
                  suffixIcon: SizedBox(
                    child: Icon(Icons.search, size: 20,),
                  ),
                ),
                style: TextStyle(fontSize: 14),
              ),
            )
          )
        ),
        Container(
          width: MediaQuery.of(context).size.width,
          margin: const EdgeInsets.only(top: 60, left: ThemeVars.paddingPage, right: ThemeVars.paddingPage),
          child: ListView(
            children: _renderList(),
          ),
        ),
      ],
    );
  }

}